package dmwr.servlet;

import java.io.IOException;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dmwr.web.Http;

public class Logout extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final Logger log = Logger.getLogger(Logout.class.getName());

	@Override
	protected void service(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		Http.request().getSession().removeAttribute("user");
		Http.request().getSession().invalidate();
		Cookie[] cookies = Http.request().getCookies();
		if (cookies != null) {
			for (Cookie cookie : cookies) {
				if ("remember".equals(cookie.getName())) {
					cookie.setValue(null);
					cookie.setMaxAge(0);
					Http.response().addCookie(cookie);
					break;
				}
			}
		}
		Http.redirect("/");
	}
}
